package com.c.ctgproject.ui.sell.parts

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.SingleCarPartAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.SingleCarPartEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_seller_sccan.*
import kotlinx.android.synthetic.main.activity_singl_car_parts.*
import kotlinx.android.synthetic.main.activity_singl_car_parts.mTitleBar

/**
 *作者:LiBW
 *创建日期: 2020/9/8
 *描述: 单辆车已销售配件
 */
class SingleCarPartsActivity : BaseActivity() {
    private val carCode: String by lazy { intent.getStringExtra(MyConstants.CAR_CODE) }

    //默认展示第一页
    private var page = 1

    //默认每页展示条数
    private var size = 5

    //待入场车辆列表信息
    private var mList: ArrayList<SingleCarPartEntity.DataBean.ListBean> = ArrayList()
    private val mAdapter: SingleCarPartAdapter by lazy { SingleCarPartAdapter(this, mList) }
    private val mViewModel: SingleCarPartsViewModel by lazy { ViewModelProvider(this)[SingleCarPartsViewModel::class.java] }

    override fun getLayout() = R.layout.activity_singl_car_parts

    override fun event() {
        changeTransparentStatusBar()
        changeStatusIconColor(true)
        mTitleBar.setTitle(carCode)
        mTitleBar.setTitleTextSize(14f)
        mSingleCarPartsRV.adapter = mAdapter
        mSingleCarPartsRV.layoutManager = LinearLayoutManager(this)
        loading.show()
        mViewModel.getPartList(carCode, page, size)
        mViewModel.partList.observe(this, Observer {
            loading.dismiss()
            mRefresh.finishLoadMore()
            mRefresh.finishRefresh()
            MyLog.d("单车配件销售$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(it, SingleCarPartEntity::class.java).data?.list
                entity?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                CustomToast.showToast(this, data.msg.handlerNull())
            }

        })

        mViewModel.error.observe(this, Observer {
            MyLog.d(it)
            loading.dismiss()
            mRefresh.finishLoadMore()
            mRefresh.finishRefresh()
            loading.dismiss()
            CustomToast.showToast(this, it)
        })
    }

    override fun click() {
        //下拉刷新
        mRefresh.setOnRefreshListener {
            mList.clear()
            page = 1
            mViewModel.getPartList(carCode, page, size)
        }
        //上啦加载
        mRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getPartList(carCode, page, size)
        }
    }
}